package br.cefet.farmaciaonline.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.cefet.farmaciaonline.connection.ConnectionFactory;
import br.cefet.farmaciaonline.vo.Paciente;

public class PacienteDAO {

	Connection con = null;
	PreparedStatement ptmt = null;
	ResultSet rs = null;
	private static PacienteDAO pacienteDao;
	
	/*
	 * public EmployeeJdbcDAO() {
	 * 
	 * }
	 */

	private Connection getConnection() throws SQLException {
		Connection conn;
		conn = ConnectionFactory.getInstance().getConnection();
		return conn;
	}
	
	public static PacienteDAO getInstance(){ 
		
		if (pacienteDao == null){
			pacienteDao = new PacienteDAO();
		}
		
		return pacienteDao;
		
	} 
	
	public void inserir(Paciente pax) {
	
		try{
			Connection conn = getConnection();		              
			if(conn != null)  {
				String querystring = "INSERT INTO paciente(userid, dataNasc, telefone) VALUES (? ,?, ?)";
				ptmt = conn.prepareStatement(querystring);		
				ptmt.setInt(1, pax.getUserid());
				ptmt.setString(2, pax.getDataNasc());
				ptmt.setString(3, pax.getTelefone());
				ptmt.executeUpdate();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}	
	}

	public void add(Paciente paciente) {

		try {
			String querystring = "INSERT INTO EMPLOYEE VALUES(?,?,?,?,?)";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);
			/*
			 * ptmt.setString(1, String.valueOf(System.currentTimeMillis()));
			 * ptmt.setString(2, paciente.getEmpName()); ptmt.setString(3,
			 * paciente.getEmpAddr()); ptmt.setString(4,
			 * paciente.getEmpEmail()); ptmt.setString(5,
			 * paciente.getEmpPhone());
			 */
			ptmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	public void update(Paciente paciente) {

		try {
			String querystring = "UPDATE Paciente SET dataNasc=?,"
					+ "telefone=? WHERE userid=?";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);

			
			 ptmt.setString(1, paciente.getDataNasc()); 
			 ptmt.setString(2, paciente.getTelefone()); 
			 ptmt.setInt(3, paciente.getUserid()); 

			ptmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	public void delete(String employeeId) {

		try {
			String querystring = "DELETE FROM EMPLOYEE WHERE EMPID=?";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);
			ptmt.setString(1, employeeId);
			ptmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	public List<Paciente> findAll() {
		List<Paciente> pacientes = new ArrayList<Paciente>();
		Paciente paciente = null;
		try {
			String querystring = "SELECT * FROM EMPLOYEE";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);
			rs = ptmt.executeQuery();
			while (rs.next()) {
				paciente = new Paciente();
				/*
				 * employeeBean.setEmpId(rs.getString(1)); Trocar por paciente
				 * employeeBean.setEmpName(rs.getString(2));
				 * employeeBean.setEmpAddr(rs.getString(3));
				 * employeeBean.setEmpEmail(rs.getString(4));
				 * employeeBean.setEmpPhone(rs.getString(5));
				 */

				pacientes.add(paciente);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return pacientes;
	}

	public Paciente getById(int id) {

		Paciente paciente = null;
		try {
			String querystring = "SELECT * FROM paciente WHERE userid=?";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);
			ptmt.setInt(1, id);
			rs = ptmt.executeQuery();
			if (rs.next()) {
				paciente =new Paciente();
				paciente.setUserid(rs.getInt(1));
				paciente.setDataNasc(rs.getString(2));
				paciente.setTelefone(rs.getString(3));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return paciente;
	}

	public static void main(String[] args) {

		PacienteDAO employeeDAO = new PacienteDAO();
		System.out.println(employeeDAO.findAll().size());
	}
}